package beer.xz.beer.device.ui.register

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import beer.xz.beer.device.data.api.ApiConfig
import beer.xz.beer.device.util.Utils.generateQRCode


@Composable
fun LoadFailScreen(navController: NavController) {
    val qrCodeImage: ImageBitmap = generateQRCode(ApiConfig.getCode() ?: "", 400)
    Box {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(top = 30.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "设备[${ApiConfig.getCode()}]加载失败，请联系管理员",
                color = Color.Red,
                fontSize = TextUnit(30f, TextUnitType.Sp),
                style = MaterialTheme.typography.headlineLarge
            )

            Image(
                bitmap = qrCodeImage,
                contentDescription = "QR Code",
                modifier = Modifier.padding(10.dp)// 设置二维码的大小
            )
            Spacer(Modifier.height(30.dp))
            Button(
                onClick = { navController.navigate("loading") },
                modifier = Modifier.align(Alignment.CenterHorizontally)
            ) {
                Text("返回", fontSize = TextUnit(30f, TextUnitType.Sp))
            }
        }
    }
}